package org.sample.project;

import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.ChangeLogParseException;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.ChangeLogParserFactory;
import liquibase.parser.core.formattedsql.FormattedSqlChangeLogParser;
import liquibase.precondition.PreconditionFactory;
import org.sample.project.core.DbFor;
import org.sample.project.custom.MyFormattedSqlChangeLogParser;
import org.sample.project.custom.MyResourceAccessor;
import org.sample.project.custom.MyTableNotExistsCheckPrecondition;
import org.sample.project.exception.NoChangesetException;

import java.sql.Connection;
import java.sql.DriverManager;

public class Appication {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            registerCustomPrecondition();
            registerCustomChangeLogParser();

            // 1. 创建数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "test");
            //connection = DriverManager.getConnection("jdbc:dm://127.0.0.1:5236?schema=BSP", "bsp", "abc@321");

            // 2. 创建 Liquibase 实例
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            Liquibase liquibase = new Liquibase("changelog.xml",
                    new MyResourceAccessor("E:/data2/MyCode/liquibase-sample/sample-project/script/",
                            "",
                            DbFor.SaaS,
                            connection), database);

            // 3. 执行数据库迁移
            liquibase.update(new Contexts(), new LabelExpression());
            //liquibase.tag("v1.2");
            liquibase.rollback(2, new Contexts(), new LabelExpression());

            System.out.println("升级成功");

        }
        catch (Exception e) {
            if(e.getCause() instanceof ChangeLogParseException && e.getCause().getCause() instanceof NoChangesetException){
                System.out.println("没有需要执行的数据库变更");
            }
            else{
                e.printStackTrace();
            }
        } finally {
            // 4. 关闭数据库连接
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void registerCustomPrecondition() {
        // 获取 PreconditionFactory 实例
        PreconditionFactory preconditionFactory = PreconditionFactory.getInstance();
        // 注册自定义 Precondition
        preconditionFactory.register(new MyTableNotExistsCheckPrecondition());
    }

    private static void registerCustomChangeLogParser() {
        // 获取 ChangeLogParserFactory 实例
        ChangeLogParserFactory parserFactory = ChangeLogParserFactory.getInstance();
        ChangeLogParser formattedSqlChangeLogParser = null;
        for(ChangeLogParser parser : parserFactory.getParsers()){
            if(parser instanceof FormattedSqlChangeLogParser){
                formattedSqlChangeLogParser = parser;
            }
        }
        if(formattedSqlChangeLogParser != null){
            parserFactory.unregister(formattedSqlChangeLogParser);
        }
        // 注册自定义 ChangeLogParser
        parserFactory.register(new MyFormattedSqlChangeLogParser());
    }
}
